package com.lmaor.project.commons.arguments;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Arguments {
	Map<String, Object> argsMap = new HashMap<>();
	public Arguments(String args[]) {
		String key = null;
		String val = null;
		for (String arg : args) {
			if(arg.charAt(0)=='-'&&arg.length()>1) {
				key = arg.substring(1);
				continue;
			}else {
				val = arg;
			}
			if(key!=null) {
				Object value = argsMap.get(key);
				if(value == null) {
					argsMap.put(key, val);
				}else if(value instanceof List) {
					((List<String>)value).add(val);
				}else {
					List<String> list = new ArrayList<>();
					list.add(value.toString());
					list.add(val);
					argsMap.put(key, list);
				}
			}
		}
	}
	
	private static String z[]= {};
	
	public String[] get(String key,String... keys){
		Object value = argsMap.get(key);
		YM:if(value == null) {
			if(keys!=null)
			for (String keyx : keys) {
				value = argsMap.get(keyx);
				if(value!=null) {
					break YM;
				}
			}
			return z;
		}
		if(value instanceof List) {
			List<String> xvalue = ((List<String>)value);
			return xvalue.toArray(new String[xvalue.size()]);
		}else {
			return new String[] {value.toString()};
		}
	}
	public String getString(String key,String... keys){
		Object value = argsMap.get(key);
		YM:if(value == null) {
			if(keys!=null)
			for (String keyx : keys) {
				value = argsMap.get(keyx);
				if(value!=null) {
					break YM;
				}
			}
			return null;
		}
		if(value instanceof List) {
			List<String> xvalue = ((List<String>)value);
			return xvalue.get(0);
		}else {
			return value.toString();
		}
	}
}
